<?php

// @formatter:off
/**
 * @file t.search.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-05-04 01:33:52 +0000 (Sun, 04 May 2014) $
 *
 * $Id: t.search.php 111 2014-05-04 01:33:52Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/tools/t.search.php $
 */
$wcToolScripts["search"] = array(
	"name" => "Search",
	"description" => "@todo",
	"class" => "WCToolSearch",
	"url" => "http://code.google.com/p/wcomix/",
	"enabled" => true
);

// @formatter:on

class WCToolSearch extends WCTool {
	//
	// Public methods.
	public function run($spacer = "") {
		global $wcOptions;

		if($wcOptions->activated("Pattern")) {
			$this->search($spacer);
		} elseif($wcOptions->activated("ShowTypes")) {
			$this->types($spacer);
		} elseif($wcOptions->activated("Index")) {
			$this->index($spacer);
		} else {
			$this->showHelp($spacer);
		}
	}
	public function showHelp($spacer = "") {
		echo "{$spacer}Options:\n";
		echo "{$spacer}\t-e pattern, --exceptions pattern\n";
		echo "{$spacer}\t\t@TODO.\n\n";
		echo "{$spacer}\t-i, --index\n";
		echo "{$spacer}\t\t@TODO.\n\n";
		echo "{$spacer}\t-l limit, --limit limit\n";
		echo "{$spacer}\t\t@TODO.\n\n";
		echo "{$spacer}\t-p pattern, --pattern pattern\n";
		echo "{$spacer}\t\t@TODO.\n\n";
		echo "{$spacer}\t-Y, --show-types\n";
		echo "{$spacer}\t\t@TODO.\n\n";
		echo "{$spacer}\t-t TYPE1,TYPE2...\n";
		echo "{$spacer}\t--types TYPE1,TYPE2,...\n";
		echo "{$spacer}\t\t@TODO.\n\n";
	}
	//
	// Protected methods.
	protected function index($spacer) {
		echo "{$spacer}Updating search index:\n";
		WCSearch::I()->updateIndex(true, "{$spacer}\t");
	}
	protected function loadOptions() {
		if(!$this->_optionsLoaded) {
			global $wcOptions;

			if($wcOptions->addValueOption("Pattern")) {
				$wcOptions->addOptionTrigger("Pattern", "p");
				$wcOptions->addOptionTrigger("Pattern", "pattern");
			}
			if($wcOptions->addValueOption("Exceptions")) {
				$wcOptions->addOptionTrigger("Exceptions", "e");
				$wcOptions->addOptionTrigger("Exceptions", "exceptions");
			}
			if($wcOptions->addValueOption("Limit")) {
				$wcOptions->addOptionTrigger("Limit", "l");
				$wcOptions->addOptionTrigger("Limit", "limit");
			}
			if($wcOptions->addValueOption("Types")) {
				$wcOptions->addOptionTrigger("Types", "t");
				$wcOptions->addOptionTrigger("Types", "types");
			}
			if($wcOptions->addOption("ShowTypes")) {
				$wcOptions->addOptionTrigger("ShowTypes", "Y");
				$wcOptions->addOptionTrigger("ShowTypes", "show-types");
			}
			if($wcOptions->addOption("Index")) {
				$wcOptions->addOptionTrigger("Index", "i");
				$wcOptions->addOptionTrigger("Index", "index");
			}

			$wcOptions->check();

			$this->_optionsLoaded = true;
		}
	}
	protected function search($spacer) {
		global $wcOptions;
		$pattern = $wcOptions->value("Pattern");
		$exceptions = false;
		$limit = false;
		$offset = false;
		if($wcOptions->activated("Exceptions")) {
			$exceptions = $wcOptions->value("Exceptions");
		}
		if($wcOptions->activated("Limit")) {
			$limit = $wcOptions->value("Limit");
		}
		$onlyTypes = array();
		if($wcOptions->activated("Types")) {
			$onlyTypes = explode(",", $wcOptions->value("Types"));
		}

		echo "{$spacer}Pattern:    '{$pattern}'\n";
		if($exceptions) {
			echo "{$spacer}Exceptions: '{$exceptions}'\n";
		}
		if($onlyTypes) {
			echo "{$spacer}Only:       ".implode(", ", $onlyTypes)."\n";
		}

		$result = WCSearch::I()->search($pattern, $exceptions, $onlyTypes, $limit, $offset);

		echo "{$spacer}".count($result)." items found:\n";



		foreach($result as $item) {
			$objItem = WCSearchFactory::ItemBuilder($item);
			if($objItem) {
				echo "{$spacer}\t* [{$item->type()}-{$item->score()}]\t{$objItem}\n";
			}
		}

		echo "{$spacer}Times:\n";
		echo "{$spacer}\tSearch:    ".WCSearch::I()->elapsedTime()." ms\n";
		if($exceptions) {
			echo "{$spacer}\tExceptions: ".WCSearch::I()->timer("exceptions")." ms\n";
			echo "{$spacer}\tFiltering:  ".WCSearch::I()->timer("filtering")." ms\n";
		}
	}
	protected function types($spacer) {
		echo "{$spacer}Available types: ".implode(", ", WCSearchFactory::Types())."\n";
	}
}

?>